<?php

class Pagination
{
	private $soDongHThi=10;
	private $trangBatDau;
	private $trangKetThuc;
	private $pageMin;
	private $pageMax;
	private $soTrang;
	private $trangHTai;//Trang hien tai.

	public function __construct(){

	}

	public function KhoiTaoTrang($trangHTai,$soDongHThi,$pageMin,$sodong)//Ham tao co doi so.
	//TRong do doi so result la ket qua truy van cua chuoi truy van du lieu.
	{
		$this->trangHTai=$trangHTai;
		$this->soDongHThi=$soDongHThi;
		$this->pageMin=$pageMin;

		$this->trangBatDau=($this->trangHTai-1)*$this->soDongHThi;
		$this->trangKetThuc=$this->trangHTai*$this->soDongHThi;
		$this->soTrang=ceil($sodong/$this->soDongHThi);
		$this->pageMax=$this->pageMin+9;
	}

	public function LayDsTrang($trangLienKet)
	{
		//Lay chuoi hien thi 1 2 3 4.
		if($this->soTrang>1)
		{
			$dsTrang="<td><b style='color:#993399;'><b></td>";
			if($this->soTrang>10)
				$dsTrang.="<td><a href='".$trangLienKet."page=1&pageMin=1'>First</a></td>";

			if($this->trangHTai==$this->pageMin&&$this->pageMin!=1)
				$this->pageMin=$this->pageMin-7;
			if($this->trangHTai==$this->pageMax)
				$this->pageMin=$this->pageMax-2;
			if($this->pageMin<=0)
				$this->pageMin=1;

			$tempPage=$this->pageMin;

			for($i=1;$i<=10;$i++)
			{
				if(($tempPage==$this->trangHTai)&&($tempPage<=$this->soTrang))
					$dsTrang.="<td class='chon'>".$tempPage."</td>";
				else
					if($tempPage<=$this->soTrang)
					$dsTrang.="<td><a href='".$trangLienKet."page=".$tempPage."&pageMin=".$this->pageMin."'>".$tempPage."</a></td>";
				$tempPage++;
			}

			if($this->soTrang>10)
				$dsTrang.="<td><a href='".$trangLienKet."page=".$this->soTrang."&pageMin=".($this->soTrang-9)."'>Last</a></td>";
		}


		return $dsTrang;
	}

	public function HThiDsTrang($dsTrang)
	{
		echo "<table id='pTrang'>";
		echo "<tr>";
		echo $dsTrang;
		echo "</tr>";
		echo "</table>";
	}

	public function gets($listPage)
	{
		return "<table id='pTrang'><tr>".$listPage."</tr></table>";
	}

	public function LayTrangBatDau()
	{
		return $this->trangBatDau;
	}

	public function LayTrangKetThuc()
	{
		return $this->trangKetThuc;
	}


}
